package com.titar.robot.http;


import android.nfc.FormatException;
import android.text.TextUtils;

import com.titar.robot.utils.NetUtils;

import org.apache.http.conn.ConnectTimeoutException;
import org.json.JSONException;

import java.net.ConnectException;
import java.net.UnknownHostException;
import java.text.ParseException;

import javax.net.ssl.SSLPeerUnverifiedException;

import retrofit2.HttpException;

public class ApiErrorResponse<T> extends ApiResponse<T> {
    private final int UNAUTHORIZED = 401; //  未授权的请求
    private final int FORBIDDEN = 403;//禁止访问
    private final int NOT_FOUND = 404;//服务器地址未找到
    private final int REQUEST_TIMEOUT = 408;//请求超时
    private final int INTERNAL_SERVER_ERROR = 500;//服务器出错
    private final int BAD_GATEWAY = 502;//无效的请求
    private final int SERVICE_UNAVAILABLE = 503;//服务器不可用
    private final int GATEWAY_TIMEOUT = 504;//网关响应超时
    private final int ACCESS_DENIED = 302;//网络错误
    private final int HANDEL_ERROR = 417;//接口处理失败

    /**
     * 未知错误
     */
    private final int UNKNOWN = 1000;
    /**
     * 解析错误
     */
    private final int PARSE_ERROR = 1001;
    /**
     * 网络错误
     */
    private final int NETWORK_ERROR = 1002;
    /**
     * 协议出错
     */
    private final int HTTP_ERROR = 1003;

    /**
     * 证书出错
     */
    private final int SSL_ERROR = 1005;

    /**
     * 连接超时
     */
    private final int TIMEOUT_ERROR = 1006;

    /**
     * 证书未找到
     */
    private final int SSL_NOT_FOUND = 1007;

    /**
     * 出现空值
     */
    private final int NULL = -100;

    private String errorMessage;

    public ApiErrorResponse(Throwable e) {
        if (e instanceof HttpException) {
            HttpException ex = (HttpException) e;
            switch (ex.code()) {
                case UNAUTHORIZED:
                    errorMessage = "未授权的请求";
                    break;
                case FORBIDDEN:
                    errorMessage = "禁止访问";
                    break;
                case NOT_FOUND:
                    errorMessage = "服务器地址未找到";
                    break;
                case REQUEST_TIMEOUT:
                    errorMessage = "请求超时";
                    break;
                case GATEWAY_TIMEOUT:
                    errorMessage = "网关响应超时";
                    break;
                case INTERNAL_SERVER_ERROR:
                    errorMessage = "服务器出错";
                    break;
                case BAD_GATEWAY:
                    errorMessage = "无效的请求";
                    break;
                case SERVICE_UNAVAILABLE:
                    errorMessage = "服务器不可用";
                    break;
                case ACCESS_DENIED:
                    errorMessage = "网络错误";
                    break;
                case HANDEL_ERROR:
                    errorMessage = "接口处理失败";
                    break;
                default:
                    if (TextUtils.isEmpty(ex.message())) {
                        errorMessage = e.getMessage();
                    }

                    if (TextUtils.isEmpty(ex.getMessage()) && e.getLocalizedMessage() != null) {
                        errorMessage = e.getLocalizedMessage();
                    }
                    if (TextUtils.isEmpty(ex.getMessage())) {
                        errorMessage = "未知错误";
                    }
                    break;
            }
        } else if (e instanceof JSONException || e instanceof ParseException) {
            errorMessage = "解析出错！";
        } else if (e instanceof ConnectException) {
            errorMessage = "连接失败";
        } else if (e instanceof javax.net.ssl.SSLHandshakeException) {
            errorMessage = "证书验证失败";
        } else if (e instanceof java.security.cert.CertPathValidatorException) {
            errorMessage = "证书路径没找到";
        } else if (e instanceof SSLPeerUnverifiedException) {
            errorMessage = "无有效的SSL证书";
        } else if (e instanceof ConnectTimeoutException) {
            errorMessage = "连接超时";
        } else if (e instanceof java.net.SocketTimeoutException) {
            errorMessage = "连接超时";
        } else if (e instanceof java.lang.ClassCastException) {
            errorMessage = "类型转换出错";
        } else if (e instanceof NullPointerException) {
            errorMessage = "数据有空";
        } else if (e instanceof FormatException) {
            errorMessage = "服务端返回数据格式异常";
        } else if (e instanceof UnknownHostException) {
            if (!NetUtils.isNetConnected()){
                errorMessage = "当前没有网络连接，请确保你已经打开网络!";
            }else {
                errorMessage = "服务器忙碌,请重试!";
            }

        } else {
            errorMessage = "未知异常！";
        }
    }

    public ApiErrorResponse(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public String getErrorMessage() {
        return errorMessage;
    }
}
